<?php
class Controller_Promotion extends  Controller_Template {
	public function __construct($req, $res) {
		$this->template = "smarty:promotion/all_activities";
		parent::__construct($req, $res);
	}

    public function action_index ()
    {
        $role = user::current_user()->get_role_id();
        if ($role == 'mmcp')
            $this->request->redirect('/mm/stat', 301);
        else if (user::passport() == 'ac.union.sososearch@cps.appchina.com')
            $this->request->redirect('/soso/yyh', 301);
        else if (user::passport() == 'ac.union.feitu@cps.appchina.com')
            $this->request->redirect('/feitu/yyh', 301);
        else if (user::passport() == 'ac.union.sw@cps.appchina.com')
            $this->request->redirect('/shunwang/yyh', 301);
        else
            $this->action_downapk();
    }

	/**
	 *
	 * 获得所有的推广活动
	 *
	 */

	public function action_all_activities()
	{
		//1 表示可以申请的活动
		$activities = Promotion_Activity::get_activities('verified', NULL, $is_leaf = 0);
		$this->template->set('activities',$activities);
	}

	/**
	 *
	 * 获得渠道商可申请的推广活动
	 *
	 */

	public function action_activities()
	{
		$mer_id = User::current_user_id();
		$this->template->set_filename('smarty:promotion/activities');
		//可申请的活动
		$activities = Promotion_Activity::get_activities('verified', NULL, 0);
		$p_vendor =  new Promotion_Vendor($mer_id);
		//已经申请的活动
		$v_activities = $p_vendor->activities();
		$has_activities = array();
		foreach($v_activities as $val)
		{
			$has_activities["$val[activity_id]"] = TRUE;
		}
		foreach($activities as $key => $val)
		{
			$rs[$key]['id'] = $val['id'];
			$rs[$key]['price'] = $val['price'];
			$rs[$key]['name'] = $val['name'];
			$rs[$key]['pay_type'] = $val['pay_type'];
			$rs[$key]['ratio'] = $val['ratio'];
			$rs[$key]['status'] = self::has_activity($mer_id,$val['id']);
			
		}
		$this->template->set('activities',$rs);
	}

	/**
	 *
	 *下载apk
	 *
	 *
	 */

	public function action_downapk()
	{
		$mer_id = User::current_user_id();
		$merchant_email = User_Contact::email($mer_id);
		$this->template->set('globa_site_type','merchant');
        $this->template->set('merchant_email', $merchant_email);
		$p_vendor =  new Promotion_Vendor($mer_id);
		//已经申请的活动
		$v_activities = $p_vendor->activities();
		$rs = array();
		foreach($v_activities as $key => $val)
		{
			$activity = Promotion_activity::get_activity_by_id($val['activity_id']);
			$channel  = Promotion_Channel::info($val['channel_id']);
            $url      = Arr::get($val, 'url', '');
            if (substr($url, 0, 4) == 'http')
                $url = trim($url);
            else
                $url = '';

			$rs[$key]['activity_name'] = $activity['name'];
			$rs[$key]['channel_name'] = $channel['name'];
			$rs[$key]['channel_id'] = $val['channel_id'];
			$rs[$key]['url'] = $url;
			$rs[$key]['key'] = $channel['unique_key'];
            $rs[$key]['promotion'] = $val;
		}
		$this->template->set_filename('smarty:promotion/downapk');
		$this->template->set('activities',$rs);
	}
	/**
	 *
	 * 进入到新建活动页面
	 *
	 */
	public function action_new_activity()
	{
		$this->template->set_filename('smarty:promotion/spread_action_four');
	}

	/**
	 *
	 * 新建一个推广活动
	 *
	 */

    public function action_create_activity()
    {
        $project = Arr::get($_POST, 'name');
        $price   = Arr::get($_POST, 'price');
        $pkgname = 'com.appchina.yyh';
        $pay_type = 'CPA';
        $pay_by   = 'newbie';
        $ratio    = Arr::get($_POST, 'ratio');
		//$activity_id 暂时没用到
        $activity_id = Promotion_Activity::create_app_project($project, $price, $pkgname, $pay_type, $pay_by, $ratio);
		$this->request->redirect('promotion/all_activities');
	
    }

	public function action_activity_detail($ac_id)
	{
		$mer_id   = User::current_user_id();
		$role_id = user::current_user()->get_role_id();
		if ($role_id == 'commonmanager')
		{
			$isCommonmanager = TRUE;
		}
		$activity = Promotion_Activity::get_activity_by_id($ac_id);
		$is_applied = self::has_activity($mer_id,$ac_id);
		$this->template->set_filename('smarty:promotion/activity_detail');
		$this->template->set('is_applied',$is_applied);
		$this->template->set('isCommonmanager',$sCommonmanager);
		$this->template->set('act',$activity);
		$this->template->set('channels_valid', Promotion::valid_channels($ac_id, $mer_id));
	}

	/**
	 *
	 * 申请推广活动
	 *
	 */
	public function action_apply_activity()
	{
		$activity_id = Arr::get($_GET,'ac_id');
		$activity_name = Arr::get($_GET,'ac_name');
		$ratio= Arr::get($_GET,'ratio');
		$this->template->set_filename('smarty:promotion/apply_activity');
		$this->template->set('ac_name',$activity_name);
		$this->template->set('ratio',$ratio);
		$this->template->set('ac_id',$activity_id);

	}
	public function action_apply_submit()
	{
		$post = $this->request->post();
		$activity_id = Arr::get($post, 'ac_id'); 
		$channel_id = Arr::get($post,'channel_id',array());
		foreach($channel_id as $val)
		{
			Promotion::add_2_activity($activity_id, $val);
		}
		$this->request->redirect('promotion/unverified_applies');
	}

	/**
	 *
	 * 获得待审核的推广申请
	 *
	 */
	public function action_unverified_applies()
	{
		$this->template->set_filename('smarty:promotion/unverified_applies');
        $result = Promotion::search(array(
            'status' => 'unverified',
            'include_activity_info' => TRUE,
            'include_channel_info'  => TRUE,
        ));
		$this->template->set('list', $result);
	}

    public function action_activity_verify()
    {
		$activity_id = Arr::get($_GET,'ac_id');
        $act = new Promotion_Activity($activity_id);
        $result = $act->verify();
		$this->request->redirect('promotion/all_activities');
    }

    public function action_applies_verify ()
	{	
		$activity_id = Arr::get($_GET,'ac_id');
		$channel_id = Arr::get($_GET,'channel_id');
        $promotion = new Promotion($activity_id, $channel_id);
        $target_url = 'http://www.sssss';
        $promotion->verify($target_url);
		$this->request->redirect('promotion/unverified_applies');
    }

	private function has_activity($merchant_id,$activity_id)
	{
		$p_vendor =  new Promotion_Vendor($merchant_id);
		$v_activities = $p_vendor->activities();	
		foreach($v_activities as $val)
		{
			if ($activity_id == $val['activity_id'])
			{
				return TRUE;
			}	
		}
		return FALSE;
	}
}
